package cn.zhoutingrui.pethome.system.emp.controller;

import cn.zhoutingrui.pethome.system.emp.domain.EmpEchart;
import cn.zhoutingrui.pethome.system.emp.domain.Employee;
import cn.zhoutingrui.pethome.system.emp.query.EmployeeQuery;
import cn.zhoutingrui.pethome.system.emp.service.IEmployeeService;
import cn.zhoutingrui.pethome.utils.AjaxResultUtil;
import cn.zhoutingrui.pethome.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/emps")
@Api(value = "员工api",description = "员工相关增删改查")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping(method = RequestMethod.GET)
    @ApiOperation("查询所有员工")
    public AjaxResultUtil findAll(){
        try {
            List<Employee> emps = employeeService.findAll();
            return AjaxResultUtil.success().setData(emps);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail().setMsg("网络异常");
        }
    }

    @RequestMapping(value = "/listPage",method = RequestMethod.POST)
    @ApiOperation("高级查询")
    public AjaxResultUtil findQuery(@RequestBody EmployeeQuery employeeQuery){
        try {
            List<Employee> emps = employeeService.findQuery(employeeQuery);
            Long count = employeeService.findByQueryCount(employeeQuery);
            return AjaxResultUtil.success().setData(emps).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail();
        }
    }

    @RequestMapping(method = RequestMethod.PUT)
    @ApiOperation("员工数据修修改,id为空是是保存")
    public AjaxResultUtil save(@RequestBody Employee employee){
        try {
            //判断数据是否为空
            if (employee.getId()==null){
                //执行添加
                employeeService.add(employee);
            }else {
                //执行修改
                employeeService.update(employee);
            }
            return AjaxResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResultUtil.fail();
    }

    @RequestMapping(value = "/{id}" ,method = RequestMethod.DELETE)
    @ApiOperation("删除一个员工")
    public AjaxResultUtil delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail().setMsg(e.getMessage());
        }
    }

    @RequestMapping(value = "/del",method = RequestMethod.PATCH)
    @ApiOperation("批量删除")
    public AjaxResultUtil batchDel(@RequestBody List<Long> ids){
        try {
            employeeService.batchDelete(ids);
            return AjaxResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResultUtil.fail();
    }

    @GetMapping("/export")
    @ApiOperation("导出")
    public void exportExcel(HttpServletResponse response){
        List<Employee> all = employeeService.findAll();
        ExcelUtil.exportExcel(all,"员工信息","表一",Employee.class,"员工数据统计.xls",response);
    }
    @PostMapping("/import")
    @ApiOperation("导入")
    public void importExcel(@RequestPart("file")MultipartFile file){
        List<Employee> employees = ExcelUtil.importExcel(file, 1, 1, Employee.class);
        employees.forEach(System.out::println);
    }

    @GetMapping("/echarts")
    @ApiOperation("echart图形数据")
    public AjaxResultUtil echart(){
        List<EmpEchart> byEchart = employeeService.findByEchart();
        return AjaxResultUtil.success().setData(byEchart);
    }
}
